let http = require("http")
let fs = require("fs")
let cheerio = require("cheerio")
let path = require('path')
let superAgent = require('superagent')
let getImages = require('./getImages')
let async = require('async')
var NewModel = require('./models/news');
// var urlList = JSON.parse(fs.readFileSync('list.json', 'utf8'))
module.exports = {
  getArticle (article, count, cb2) {
    superAgent
    .get(article.link)
    .set('Connection', 'keep-alive')
    .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')
    .end((err, res) => {
      if (err || !res.ok) {
         console.log('出错了')
         cb2(false)
      } else {
        // sres.text 里面存储着网页的 html 内容，将它传给 cheerio.load 之后
        // 就可以得到一个实现了 jquery 接口的变量，我们习惯性地将它命名为 `$`
        // 剩下就都是 jquery 的内容了
        var $ = cheerio.load(res.text);
        async.waterfall([
          (cb3) => {
            let title = $('.post_title h1').text()
            if (!title) {
              cb3(false, [])
              return
            }
            // 解析图片，存到本地
            let $img = $('#paragraph img')
            let imgs = []
            if ($img) {
              $img.each((index, img) => {
                let imgUrl = $(img).attr('data-original')
                let r = imgUrl.match(/(http:\/\/)?\w+\.(jpg|jpeg|gif|png)/)
                imgs.push({
                  name: r[0],
                  url: imgUrl
                })
                $(img).attr('src', '/news/' + r[0])
              })
            }
            let content = $('#paragraph').html().replace(/<script.*<\/script>/g, '')
            let pdate = $('#pubtime_baidu').text()
            let author = $('#author_baidu strong').text()
            NewModel.create({
              title,
              content,
              pdate,
              author
            })
            .then(function (result) {
              console.log('薅完第' + count + '篇')
              cb3(false, imgs)
            })
            .catch((err) => {
              cb3(false, [])
            })
          },
          (imgs, cb3) => { // 把图片存到本地库
            if (imgs.length > 0) {
              async.eachSeries(imgs, (item) => {
                // GetArticle.getArticle(item, ++count, callback)
                // callback()
                getImages.go(item)
              }, (res) => {
                if (res) {
                  console.log('成功弄到一张图')
                } else {
                  console.log('没能弄到图')
                }
                cb3(true)
              })
            }
            cb3()
          }
        ], (res) => {
          cb2()
        })
      }
    })
  }
}
